//后端接口地址。
const baseUrl = "http://localhost:8080/cruddemo/";
//前端的用户业务逻辑接口,这里封装了用户的所有操作

//1.查询所有的用户
function queryAllUsers() {
    const promise = new Promise((resolve, reject) => {
        $.ajax({
            url: baseUrl + 'users/all',
            type: 'GET',
            dataType: 'json',
            async: true,
            success: resp => {
                resolve(resp);
            },
            error: err => {
                //console.log(err);
                reject(err);
            }
        })
    })
    return promise;
}

//2.根据用户ID查询用户资料
function queryUserByUid(uid) {
    const promise = new Promise((resolve, reject) => {
        $.ajax({
            url: baseUrl + 'users/?uid='+uid,
            type: 'GET',
            dataType: 'json',
            async: true,
            success: resp => {
                resolve(resp);
            },
            error: err => {
                //console.log(err);
                reject(err);
            }
        })
    })
    return promise;
}

//3.新增用户
function saveUser(user) {
    const promise = new Promise((resolve, reject) => {
        $.ajax({
            url: baseUrl + 'users/',
            type: 'POST',
            dataType: 'json',
            contentType: 'application/json',
            data: JSON.stringify(user), //json对象转换为字符串。
            success: resp => {
                resolve(resp);

            },
            error: err => {
                reject(err);
            }
        })
    })
    return promise;
}

//4.更新用户
function updateUser(user) {
    const promise = new Promise((resolve, reject) => {
        $.ajax({
            url: baseUrl + 'users/',
            type: 'PUT',
            dataType: 'json',
            contentType: 'application/json',
            data: JSON.stringify(user), //json对象转换为字符串。
            success: resp => {
                resolve(resp);

            },
            error: err => {
                reject(err);
            }
        })
    })
    return promise;
}

//删除用户
function removeUser(uid) {
    const promise = new Promise((resolve, reject) => {
        $.ajax({
            url: baseUrl + 'users/?uid='+uid,
            type: 'DELETE',
            dataType: 'json',
            async: true,
            success: resp => {
                resolve(resp);
            },
            error: err => {
                //console.log(err);
                reject(err);
            }
        })
    })
    return promise;
}

//根据当前页号查询指定分页的用户数据

function queryPagerUsers(currentPagerNumber){
    const promise = new Promise((resolve, reject) => {
        $.ajax({
            url: baseUrl + 'users/pager?currentPagerNumber='+currentPagerNumber,
            type: 'GET',
            dataType: 'json',
            async: true,
            success: resp => {
                resolve(resp);
            },
            error: err => {
                //console.log(err);
                reject(err);
            }
        })
    })
    return promise;
}

//批量删除用户
function removeBatchUsers(ids){
    const promise = new Promise((resolve, reject) => {
        $.ajax({
            url: baseUrl + 'users/batch',
            type: 'DELETE',
            dataType: 'json',
            contentType: 'application/json',
            async: true,
            data: JSON.stringify(ids),
            success: resp => {
                resolve(resp);
            },
            error: err => {
                //console.log(err);
                reject(err);
            }
        })
    })
    return promise;
}

